from typing import Dict
import os

DISKIO_SCRIPT_RULES = [
    {"script": "diskio_IO延迟飙升.sh", "keywords": ["io延迟", "延迟飙升", "latency"], "weight": 2},
    {"script": "diskio_IO吞吐量瓶颈.sh", "keywords": ["吞吐量", "throughput", "瓶颈"], "weight": 2},
    {"script": "disk_磁盘空间耗尽.sh", "keywords": ["磁盘空间", "空间耗尽", "disk full"], "weight": 2},
    {"script": "disk_Inode耗尽.sh", "keywords": ["inode", "inode耗尽"], "weight": 2},
    {"script": "diskio_IO Hang.sh", "keywords": ["io hang", "hang", "卡死"], "weight": 1}
]

def select_script_for_anomaly(anomaly: Dict) -> str:
    root_cause = anomaly.get("root_cause", "").lower()
    evidence = " ".join(anomaly.get("evidence", [])).lower()
    severity = anomaly.get("severity", 0)
    best_score = -1
    best_script = DISKIO_SCRIPT_RULES[0]["script"]
    for rule in DISKIO_SCRIPT_RULES:
        score = 0
        for kw in rule["keywords"]:
            if kw in root_cause or kw in evidence:
                score += rule["weight"]
        if severity >= 8:
            score += 2
        elif severity >= 6:
            score += 1
        score += len(anomaly.get("evidence", [])) * 0.5
        if score > best_score:
            best_score = score
            best_script = rule["script"]
    return best_script 